package com.supersit.greensproject.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.BaseSynchroizationAdapter;
import com.supersit.greensproject.adapter.MaketSynchroizationAdapter;
import com.supersit.greensproject.adapter.PriceGatherSynchroizationAdatper;
import com.supersit.greensproject.entity.BaseBase;
import com.supersit.greensproject.entity.MaketSubject;
import com.supersit.greensproject.entity.PriceGather;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.utils.T;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/9 0009.
 */
@ContentView(R.layout.activity_pricegather_synchroization)
public class SynchroizationPriceGatherActivity extends BaseActivity {
    private Context context;
    private MyTitleBar myTitleBar;
    private List<PriceGather> priceGathersList;
    @ViewInject(R.id.lv_selected_data)
    private ListView lvSelectedData;
    @ViewInject(R.id.btn_synchronization)
    private Button btnSynchronization;
    @ViewInject(R.id.cb_selected)
    private CheckBox cbSelect;
    private PriceGatherSynchroizationAdatper adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        initVariable();
        bindListener();
    }

    private void setTitleBar() {
        myTitleBar = new MyTitleBar(context, findViewById(R.id.synchronizationn_title));
        myTitleBar.setTitleText(context.getResources().getString(R.string.synchronizationn));
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgRight());
    }

    private void bindListener() {
        myTitleBar.setImg1Listener(onClickListener);
        cbSelect.setOnCheckedChangeListener(onCheckedChangeListener);
        btnSynchronization.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
                case R.id.btn_synchronization:
                    subSynchroization();
                    break;
            }
        }
    };

    private void initVariable() {
        adapter = new PriceGatherSynchroizationAdatper(priceGathersList, context);
        lvSelectedData.setAdapter(adapter);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            priceGathersList = (List<PriceGather>) bundle.getSerializable("list");
            int baseType = bundle.getInt("baseType");
            myTitleBar.setTitleText(Resources.baseSynchroization[baseType]);
            if (null != priceGathersList && priceGathersList.size() > 0) {
                adapter.setData(priceGathersList);
                adapter.notifyDataSetChanged();
            }
            //防止点击同步时出现空指针
            for (int i = 0; i < (priceGathersList == null ? 0 : priceGathersList.size());
                 i++) {
                adapter.getIsSelected().put(i, false);
            }
            // 刷新listview和TextView的显示
            adapter.notifyDataSetChanged();
        }
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                // 遍历list的长度，将MyAdapter中的map值全部设为true
                for (int i = 0; i < priceGathersList.size(); i++) {
                    adapter.getIsSelected().put(i, true);
                }
                // 刷新listview和TextView的显示
                adapter.notifyDataSetChanged();
            } else {
                for (int i = 0; i < priceGathersList.size(); i++) {
                    adapter.getIsSelected().put(i, false);
                }
                // 刷新listview和TextView的显示
                adapter.notifyDataSetChanged();
            }
        }
    };

    private void subSynchroization() {
        List<PriceGather> list = new ArrayList<PriceGather>();
        for (int i = 0; i < priceGathersList.size(); i++) {
            if (adapter.getIsSelected().get(i)) {
                list.add(priceGathersList.get(i));
            }
        }
        T.showShort(context, list.size() + "");
    }
}
